#!/usr/bin/perl

# Make a snapshot of pcache files, presumably in a working state.

my $snapshotBase = "/home/dbooth/pcache-snapshots";

my @filesOrDirectories = qw(
	/etc/apache2/sites-enabled/000-default
	/home/dbooth/rdf-pipeline 
	/home/dbooth/personal/websites/dbooth.org/2011/pipeline/Booth_David-pipelines.odp
	);

my @excludeDirectories = qw ( 
	backups 
	tbc
	.svn
	);

############ Main ################

-d $snapshotBase or mkdir $snapshotBase or die;
# my $date = `date +%F`;
my $date = `date '+%Y-%m-%d-%H%M%S'`;
chomp $date;
my $dir = "$snapshotBase/snapshot-$date";
# warn "dir: $dir\n";
die "ERROR: Directory already exists: $dir\nWait at least 1 second before making another snapshot\n"
	if -e $dir;
mkdir $dir or die;
my $root = "$dir/root";
# warn "root: $root\n";
mkdir $root or die;
my $readme = "$dir/ReadMe.txt";
# warn "readme: $readme\n";
my $cmd = "( echo '### Snapshot $date ###' ; echo ; echo ) > $readme";
# warn "cmd: $cmd\n";
my $result = `$cmd`;
# chomp $result;
die $result if $result;

# Copy all the files/directories:
foreach my $f (@filesOrDirectories)
	{
	my $cmd = "cp -rp --parents '$f' '$root'";
	# warn "  cmd: $cmd\n";
	my $result = `$cmd`;
	# chomp $result;
	die $result if $result;
	}

# Delete excluded directories:
foreach my $ex (@excludeDirectories)
	{
	my $cmd = "rm -rf `find '$root' -name '$ex' -print`";
	# warn "  cmd: $cmd\n";
	my $result = `$cmd`;
	# chomp $result;
	die $result if $result;
	}

# Add an explanation in the ReadMe.txt for this snapshot:
# system("vi '+/$p' @files");
$cmd = "vi '+\$' '$readme'";
# warn "cmd: $cmd\n";
system($cmd);

